<template>
  <div class="draggable_content">
    <div class="header">
      <span>{{ props.title }}</span>
    </div>
    <div class="content">
      <draggable v-model="data.list" group="people" @start="drag = true" @end="onEnd" item-key="id">
        <template #header></template>
        <template #item="{ element, index }">
          <div :class="`time_box ${isNormal(element)}`" :id="element.templateId">
            <div
              class="child"
              @click="handelClickChildItem(element, index)"
              :class="element.templateId == data.num ? 'active' : ''"
            >
              <div class="top">
                <div class="top-left jzt-flex-center">
                  <div class="name text-over" style="max-width: 130px" @click.stop>
                    <el-input
                      v-model="element.budgetName"
                      ref="autoFocus"
                      maxlength="15"
                      v-if="element.edit"
                      @blur="handelBlur(element)"
                      @focus="onFocus"
                    ></el-input>
                    <span v-else :title="element.budgetName">{{ element.budgetName }}</span>
                  </div>
                  <img v-if="element.softDecorationStatus === 1" src="@/assets/images/ruanzhuang.png" width="36" alt="" />
                </div>
                <div class="top-right">
                  <el-switch :model-value="element.status" @change="handelChange(element)" />
                </div>
              </div>
              <div class="botton">
                <div class="botton_left">{{ element.editTime }}</div>
                <div class="botton_right">
                  <el-popover placement="bottom" title="" :width="200" trigger="hover" :popper-class="'template-actionmore'">
                    <template #default>
                      <div class="action-wrap">
                        <p v-for="(item, i) in data.operateList" :key="i" @click="handelClickOperate(element, item)">
                          <el-button link :type="item.command === 'd' ? 'danger' : 'default'">{{ item.name }}</el-button>
                        </p>
                      </div>
                    </template>
                    <template #reference>
                      <span class="el-dropdown-link">
                        <el-icon><MoreFilled /></el-icon>
                      </span>
                    </template>
                  </el-popover>
                  <!-- <el-dropdown>
                  <span class="el-dropdown-link">
                    <el-icon><MoreFilled /></el-icon>
                  </span>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item
                        command="a"
                        v-for="(item, i) in data.operateList"
                        :key="i"
                        @click="handelClickOperate(element, item)"
                        >{{ item.name }}</el-dropdown-item
                      >
                    </el-dropdown-menu>
                  </template>
                </el-dropdown> -->
                </div>
              </div>
            </div>
            <div v-if="!!element.isSetTiming" class="time_text">
              {{ `定时：${element.timingStart} ~ ${element.timingEnd}` }}
            </div>
          </div>
        </template>
      </draggable>
    </div>
  </div>
</template>
<script setup>
// 资源导入
import { reactive, defineProps, defineEmits, onMounted, watch, ref, nextTick } from "vue";
import draggable from "vuedraggable";
import more from "@/static/images/more.png";
// 变量定义
const autoFocus = ref();
const emits = defineEmits(["blurs", "deleteUp", "copy", "cgStatus", "clickItem", "dragEnd"]);
const props = defineProps({
  title: {
    type: String,
    default() {
      return "";
    }
  },
  list: {
    type: Array,
    default() {
      return [];
    }
  },
  templateId: {
    type: String,
    default() {
      return "";
    }
  },
  PartView: {
    type: Boolean,
    default: true
  }
});
const data = reactive({
  list: [],
  // 选中的数据
  selectedData: {},
  num: -1,
  childNum: -1,
  // 拖动列表数据
  myArray: [],
  operateList: [
    { id: 4, name: "设置定时模板", command: "e" },
    { id: 2, name: "编辑", command: "a" },
    { id: 3, name: "复制为套餐模板", command: "b" },
    { id: 4, name: "复制为清单模板", command: "c" },
    { id: 4, name: "删除", command: "d" }
  ],
  operateListChildFile: [
    { id: 2, name: "编辑", command: "b" },
    { id: 3, name: "复制", command: "c" },
    { id: 4, name: "删除", command: "d" }
  ],
  engineeringTypeList: []
});

const onFocus = event => {
  event.target.select();
  // console.log(909090, event);
};

const onEnd = () => {
  // drag = false
  console.log("拖拽结束left", data.list);
  emits("dragEnd", data.list);
};

// 更多操作
const handelClickOperate = (element, item) => {
  const command = item.command;
  if (command === "b") {
    emits("copy", element, 2);
  } else if (command === "a") {
    element.edit = true;

    nextTick(() => {
      // autoFocus.value.select();
      // autoFocus.value.focus();
    });
  } else if (command === "d") {
    emits("deleteUp", element);
  } else if (command === "c") {
    emits("copy", element, 1);
  } else if (command === "e") {
    emits("setTime", element);
  }
  console.log(element);
};
// 失去焦点时保存当前文件的名称并不让在编辑
const handelBlur = element => {
  element.isEdit = false;
  emits("blurs", element);
};
const handelClickChildItem = (item, i, isInitEdit = true, isPartView = true) => {
  data.num = item.templateId;
  emits("clickItem", item, i, isInitEdit, isPartView);
};
// 开关
const handelChange = item => {
  emits("cgStatus", item);
};
// 生命周期
onMounted(() => {
  //   getFilieList();
});
watch(
  () => props.list,
  arr => {
    data.list = JSON.parse(JSON.stringify(arr));

    // console.log(arr, "获取的数据list文件");
    if (props.templateId && arr.length > 0) {
      const obj = arr.find(item => item.templateId === props.templateId);
      if (obj) {
        handelClickChildItem(obj, null, false, props.PartView);
      }
    }
  },
  { deep: true, immediate: true }
);

const isNormal = item => {
  let className = "";
  if (item.timingStart && item.timingEnd) {
    const date1 = new Date(item.timingStart);
    const date2 = new Date(item.timingEnd);
    const current = new Date();
    if (current.getTime() > date1.getTime() && current.getTime() < date2.getTime()) {
      className = "normal";
    } else {
      className = "limit";
    }
  }
  return className;
};

watch(
  () => props.templateId,
  value => {
    data.num = value;
  },
  { deep: true, immediate: true }
);
// 监听
</script>

<style lang="scss">
.template-actionmore {
  min-width: auto !important;
  width: 120px !important;
  border-radius: 6px !important;
  box-sizing: border-box !important;
  .el-button {
    width: 100%;
    justify-content: flex-start !important;
    line-height: 26px !important;
    font-size: 12px !important;
  }

  .action-wrap {
    .el-button.el-button--default.is-link {
      &:hover {
        color: #007dff;
      }
    }
    .el-button.el-button--danger.is-link {
      &:hover {
        color: red;
      }
    }
  }
}
</style>
<style scoped lang="scss">
.active {
  border: 2px solid #007dff !important;
  box-sizing: border-box;
  :deep(.el-dropdown-link) {
    color: #007dff;
  }
}
.active1 {
  background-color: #d9e8ff !important;
}
.file_operate {
  display: flex;
  justify-content: space-between;
  width: 100%;
  &_right {
    display: flex;
    align-items: center;
  }
}
.time_box {
  margin-bottom: 10px;
  border-radius: 10px;
  .time_text {
    font-size: 10px;
    text-align: center;
    padding-bottom: 5px;
  }
}
.normal {
  background: #a7d2ff;
}
.limit {
  background: #ffc86a;
}
.child {
  background-color: #fff;
  margin-bottom: 5px;
  box-shadow: 0px 0px 10px 0px rgba(134, 144, 156, 0.1);
  border-radius: 10px 10px 10px 10px;
  padding: 16px;
  overflow: hidden;
  box-sizing: border-box;
  //   cursor:all-scroll;
  .top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .top-left {
      font-size: 14px;
      color: #191919;
    }
  }
  .botton {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .botton_left {
      font-size: 12px;
      color: #86909c;
    }
  }
}
.draggable_content {
  //   height: calc(100% - 40px);
  //   background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  .header {
    font-size: 18px;
    color: #131414;
    // padding: 20px;
    overflow: hidden;
    span {
      position: relative;
      font-size: 14px;
      color: #86909c;
    }
  }
}
// 拖拽外层内容css
.content {
  margin-top: 20px;
  height: calc(100% - 90px);
  overflow: scroll;
}
.file {
  width: 100%;
  height: 44px;
  display: flex;
  align-items: center;
  cursor: pointer;
  box-sizing: border-box;
  img {
    width: 16px;
  }
  span {
    margin-left: 10px;
  }
}
.child_file {
  padding-left: 40px;
  width: 100%;
  height: 44px;
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #000;
  box-sizing: border-box;
  span {
    margin-left: 10px;
  }
  img {
    width: 16px;
  }
}
.add_file {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 10px;
}
</style>
